package mvc;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * Project #4
 * CS 2334, 010
 * 04/02/2014
 * <P>
 * This class represents the model for the region data
 * </P>
 * @version 1.0
 */
public class RegionModel extends geographer.Database
{
	/*The list of listeners for the user actions*/
	ArrayList<ActionListener> listeners;
	
	/**
	 * The constructor for the region model.
	 */
	public RegionModel(){super();}//end constructor
	
	/**
	 * Adds an action listener to process user action.
	 * @param l   The action listener being added.
	 */
	public synchronized void addActionListener(ActionListener l){
		if(listeners == null)
		{
			listeners = new ArrayList<ActionListener>();
		}
		listeners.add(l);
	}//end addActionListener
	
	/**
	 * Process the Events
	 * @param e	The ActionEvent to be processed
	 */
	
	@SuppressWarnings("unchecked")
	public void processEvent(ActionEvent e) {
		ArrayList<ActionListener> list;
		
		synchronized (this) {
			if (listeners == null) return;
			list = (ArrayList<ActionListener>) listeners.clone();
		}
		
		for (int i = 0; i < list.size(); i++) {
			ActionListener listener = list.get(i);
			listener.actionPerformed(e);
		}
	}
	
	/**
	 * Add the region to the database
	 * @param	r	The region to be added
	 * @param	data	Some parameters for the region
	 */
	//@Override
	public void add(Region r, String[] data){
		super.add(r, data);
		processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, r+" add"));
	}
	
	/**
	 * Deletes the region from the database
	 * @param	reg	The Region we want to delete
	 * @param	key	The key of the region
	 */
	@Override
	public void delete(Region reg, String key)
	{
		super.delete(reg, key);
		processEvent(new ActionEvent(this, 0, reg+" delete "+key));
	}
	
	/**
	 * Edit the region
	 * @param reg	The region we want to delete
	 * @param	data	Data we want to edit the model with
	 * @param	key	The key of the region
	 */
	public void edit(Region reg, String[] data, String key)
	{
		super.edit(reg, data, key);
		processEvent(new ActionEvent(this, 0, reg+" edit "+key));
	}
	
	
}
